package fun.hijklmn.magic.swagger.config;

import fun.hijklmn.magic.swagger.properties.MagicSwaggerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description
 * @Author fs
 * @Date 2021/5/6 11:24
 * @Version 0.0.1-SNAPSHOT
 **/
@Configuration
public class MagicSwaggerConfig {

    @Autowired(required = false)
    private MagicSwaggerProperties magicSwaggerProperties;

    @Bean
    @ConditionalOnMissingBean(MagicSwaggerProperties.class)
    public Docket createInvalidRestApi() {
        Docket docket = new Docket(DocumentationType.OAS_30).select().paths(PathSelectors.none()).build();
        return docket;
    }

    @Bean
    @ConditionalOnBean(MagicSwaggerProperties.class)
    public Docket createRestApi() {

        Docket docket = new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(magicSwaggerProperties.getBasePackage())).paths(PathSelectors.any()).build();

        List<RequestParameter> requestParameters = new ArrayList<>();

        if (!CollectionUtils.isEmpty(magicSwaggerProperties.getHeaders())) {

            magicSwaggerProperties.getHeaders().forEach(header -> {

                String[] headParams = header.split("-");

                RequestParameter requestParameter = new RequestParameterBuilder().in(ParameterType.HEADER).name(headParams[0]).description(headParams[1]).required(false).build();

                requestParameters.add(requestParameter);

            });

        }

        if (!CollectionUtils.isEmpty(magicSwaggerProperties.getCookies())) {

            magicSwaggerProperties.getCookies().forEach(cookie -> {

                String[] cookieParams = cookie.split("-");

                RequestParameter requestParameter = new RequestParameterBuilder().in(ParameterType.COOKIE).name(cookieParams[0]).description(cookieParams[1]).required(false).build();

                requestParameters.add(requestParameter);

            });

        }

        if (!CollectionUtils.isEmpty(requestParameters)) {
            docket.globalRequestParameters(requestParameters);
        }

        return docket;

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(magicSwaggerProperties.getTitle()).version(magicSwaggerProperties.getVersion()).description(magicSwaggerProperties.getDesc()).build();
    }

}
